package com.training1.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.training1.core.common.global.Global;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.security.MD5;
import com.training1.core.dto.APIResultDto;
import com.training1.core.dto.HelpDto;
import com.training1.core.entity.HelpEntity;
import com.training1.core.entity.MemberEntity;
import com.training1.core.entity.SystemParamEntity;
import com.training1.core.modules.dao.SystemSettingsDao;
import com.training1.core.modules.service.HelpService;
import com.training1.core.modules.service.MemberService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "/api/help", description = "帮助中心")
// Swagger annotation
@RestController
@RequestMapping("/api/help")
public class APIHelpController {
	private static final Logger logger = LoggerFactory
			.getLogger(APIHelpController.class);

	@Resource
	private HelpService helpService;

	@ApiOperation(value = "获取帮助内容列表, 参数是关键字")
	@RequestMapping(value = "/getHelpList", method = RequestMethod.GET)
	public APIResultDto<Object> getHelpList(@RequestParam String keyWord) {
		logger.info("获取帮助内容列表, keyWord : " + keyWord);
		APIResultDto<Object> dto = new APIResultDto<Object>();

		List<HelpEntity> list = helpService.findListByKeyWord(keyWord);
		if (list != null) {
			// 如果能查询到帮助内容结果
			Map<String, Object> param = new HashMap<String, Object>();

			List<HelpDto> helpList = new ArrayList<HelpDto>();
			for (HelpEntity help : list) {
				HelpDto hd = new HelpDto();
				hd.setTitle(help.getTitle());
				hd.setFaqid(help.getId());
				helpList.add(hd);
			}
			param.put("helpList", helpList);

			dto.setData(param);
			dto.setStatus(APIResultDto.SUCCESS_OK);
			dto.setErrorCode("0");
			dto.setMessage("查询成功");
			return dto;
		} else {
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("1");
			dto.setMessage("暂无帮助内容，可拨打电话：400-179-1580进行人工咨询");
			return dto;
		}
	}

	@ApiOperation(value = "获取帮助内容详情, 参数是记录ID")
	@RequestMapping(value = "/getHelpDetail", method = RequestMethod.GET)
	public APIResultDto<Object> getHelpDetail(@RequestParam String id) {
		logger.info("获取帮助内容列表, id : " + id);
		APIResultDto<Object> dto = new APIResultDto<Object>();

		HelpEntity help = helpService.findHelpDetailById(id);
		if (null != help) {
			// 如果能查询到帮助内容结果
			Map<String, Object> param = new HashMap<String, Object>();
			HelpDto helpDetail = new HelpDto();

			helpDetail.setTitle(help.getTitle());
			helpDetail.setContent(help.getContent());
			
			param.put("helpDetail", helpDetail);
			
			dto.setData(param);
			dto.setStatus(APIResultDto.SUCCESS_OK);
			dto.setErrorCode("1");
			dto.setMessage("查询成功");
			return dto;
		}else {
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("0");
			dto.setMessage("无法取得该帮助内容");
			return dto;
		}

	}

}